package com.yusys.controller;

import com.yusys.domain.TbTest;
import com.yusys.service.TbTestService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

public class RegisterServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        String name = request.getParameter("name");
        String email = request.getParameter("email");
        String age = request.getParameter("age");

//        String config = "application-context.xml";
//        ApplicationContext ac = new ClassPathXmlApplicationContext(config);
        ServletContext sc = getServletContext();
        WebApplicationContext ac = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
        TbTestService tbTestService = (TbTestService) ac.getBean("tbTestService");

        TbTest tbTest = new TbTest();
        tbTest.setName(name);
        tbTest.setTestName(email);
        tbTestService.save(tbTest);

        request.getRequestDispatcher("/result.jsp").forward(request, response);

    }
}
